import { Component } from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router'
import {UserService} from './user.service';
import axios from 'axios';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'free4question';
  activeDom = 1;
  user
  constructor(private UserService: UserService, private router: Router) {
    //this.user = UserService.user;
    //this.user =  JSON.parse(window.localStorage.getItem("user") || '{}' );
    console.log('app component')

  }
  
  addActive(index){
    this.activeDom= index;
  }

  changeOfRoutes(){
    if(this.router.url === '/'){
      this.getCurrentUser();
    }
  }


  public getCurrentUser(){
    let baseUrl = "http://localhost:8999";

     axios.get(baseUrl+'/user/getCurrentUser', {withCredentials: true}).then(res=>{
      this.user = res.data;
      console.log("UserService")
      }).catch((err)=>{
        console.log(err);
     }).finally(()=>{
        console.log('执行完了');
     });
  }

  public logout(){
      let baseUrl = "http://localhost:8999";

      axios.get(baseUrl+'/user/logout', {withCredentials: true}).then(res=>{
        if (res.data){
          this.user = null;
          alert("注销成功");
        }else{
          alert("注销失败");
        }

      }).catch((err)=>{
        console.log(err);
      }).finally(()=>{
        console.log('执行完了');
      });
  }



}
